
<template>
  <div>
    <el-form

      v-loading="loading"
      element-loading-text="加载中...."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :rules="rules"
      ref="loginForm"
      :model="loginForm"
      class="loginContainer"
    >
      <h3 class="loginTitle">系统登录</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
          @keydown.enter.native="submitLogin"
        ></el-input>
      </el-form-item>
      <el-form-item prop="captcha" >
        <el-input
          class="captcha"
          type="text"
          v-model="loginForm.captcha"
          placeholder="点击图片更换验证码"
          @keydown.enter.native="submitLogin"
        ></el-input>
        <img :src="captchaUrl" alt="" @click="updataCaptcha" />
      </el-form-item>
      <el-button type="primary" style="width: 100%" @click.prevent="submitLogin"
        >登录</el-button>
      <el-form-item>
        <el-button type="danger" style="width: 100%;margin-top: 10px" @click="showRegisterView"
        >点击注册</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        title="添加用户"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <el-row>
          <el-form :rules="rules2"  ref="registerForm" :model="registerForm" >
              <el-form-item  label="用户名:" prop="username" style="margin-right: 10px">
                <el-input v-model="registerForm.username" placeholder="请输入用户名" ></el-input>
              </el-form-item>
            <el-form-item  label="姓名:" prop="name" style="margin-right: 10px">
              <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
              <el-form-item label="密码 :" prop="password" style="margin-right: 10px">
                <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
              </el-form-item>
            <el-form-item label="确认密码 :" prop="password"  style="margin-right: 10px">
              <el-input v-model="registerForm.password1" type="password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item label="性别:" prop="sex" style="margin-right: 10px">
              <el-radio v-model="registerForm.sex" label="男">男</el-radio>
              <el-radio v-model="registerForm.sex" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="电话 :" prop="phone"  style="margin-right: 10px">
              <el-input v-model="registerForm.phone"  placeholder="请输入电话号码"></el-input>
            </el-form-item>
         <el-button @click="dialogVisible = false">取 消</el-button>
       <el-button type="primary" @click.prevent="doAddRegister">确 定</el-button>

          </el-form>
        </el-row>
      </div>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        username: "admin",
        password: "123",
        captcha: "",
      },
      dialogVisible:false,
      loading: false,
      checked: true,
      registerForm:{
        id:'0',
        username:'test',
        password:'123',
        password1:'123',
        phone:'18859636965',
        name:'测试',
        sex:'女',
        enabled:'true',
      },
      rules2:{
        phone: [
          { min: 11, max: 11, message: "请输入11位手机号码", trigger: "blur" },
          {
           pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号码",
          },
          { required: true, message: "请输入联系方式", trigger: "blur" },
        ],
        username: [
            { required: true,message: "请输入用户名", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],

      },
    };
  },
  mounted() {
    this.dialogVisible=false;
  },
  methods: {

    showRegisterView(){
      this.dialogVisible=true;
      
    },
    doAddRegister(){
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          if (this.registerForm.password === this.registerForm.password1) {
            this.registerForm.enabled = true;
            this.postRequest("/register", this.registerForm).then(resp => {
              if (resp) {
                this.dialogVisible = false;
              }
            })
          } else {
            this.$message.error("两次输入的密码不一致");
          }
        }else {
          this.$message.error("请输入全部信息");
        }

      });
    },
    updataCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {

        if (valid) {
          this.loading = true;
          this.postRequest("/login", this.loginForm).then((resp) => {
            if (resp) {
              this.loading = false;
              const tokenStr = resp.obj.tokenHead + resp.obj.token;
              window.sessionStorage.setItem("tokenStr", tokenStr);
              let path = this.$route.query.redirect;
              this.$router.replace(
                path === "/" || path === undefined ? "/home" : path
              );
            } else {
              this.loading = false;
              this.loginForm.captcha = "";
              this.captchaUrl = "/captcha?time=" + new Date();
            }
          });
        } else {
          this.$message.error("请输入全部信息");

          return false;
        }
      });
    },
  },
};
</script>

<style>
.RegisterView{
  width: 100px;
  text-align: center;
}
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6ca;
}
.loginTitle {
  padding: 0 40px;
  text-align: center;
}

.captcha {
  width: 250px;
  margin-right: 10px;
}
.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>